#include "ThriftServer.hpp"

ThriftServer::ThriftServer(ControlCollege *college_, ClientPub *broadcast_,
                           const std::string &addr)
    : college(college_), broadcast(broadcast_), peerAddr(addr) {}

void ThriftServer::StartShot(const std::string &uuid) {
  college->StartShot(uuid);
  broadcast->StartShot(uuid);
}

void ThriftServer::StopShot(const std::string &uuid) {
  college->StopShot(uuid);
  broadcast->StopShot(uuid);
}

bool ThriftServer::ShotStatus(const std::string &uuid) {
  return college->ShotStatus(uuid);
}

void ThriftServer::GetAllDevice(std::set<std::string> &_return) {
  for (const std::string &i : college->GetAllDevice())
    _return.insert(i);
}

void ThriftServer::GetAllFormat(std::map<int16_t, std::string> &_return) {
  int countid = 0;
  for (const std::string &i : college->GetAllFormat())
    _return.insert({countid++, i});
}

void ThriftServer::GetSettingList(std::string &_return,
                                  const std::string &uuid) {
  _return = college->GetSettingList(uuid);
}

void ThriftServer::GetSettingValue(std::string &_return,
                                   const std::string &uuid,
                                   const int32_t setid) {
  _return = college->GetSettingValue(uuid, setid);
}

bool ThriftServer::SetSettingValue(const std::string &UUID,
                                   const std::string &value) {
  return college->SetSettingValue(UUID, value);
}

void ThriftServer::GetShareMemInfo(std::string &_return,
                                   const std::string &uuid) {
  _return = college->GetShareMemInfo(uuid);
}

void ThriftServer::OfflineCamera(const std::string &uuid) {
  college->Offline(uuid);
}

void ThriftServer::StartRecord(const std::string &UUID,
                               const std::string &savepath,
                               const int16_t format_index) {
  college->StartRecord(UUID, savepath, format_index);
}

void ThriftServer::StopRecord(const std::string &UUID) {
  college->StopRecord(UUID);
}

bool ThriftServer::RecordStatus(const std::string &UUID) {
  return college->RecordStatus(UUID);
}

void ThriftServerCloneFactory::releaseHandler(Thrift::NodeClientIf *handler) {
  delete handler;
}

Thrift::NodeClientIf *ThriftServerCloneFactory::getHandler(
    const ::apache::thrift::TConnectionInfo &connInfo) {
  std::shared_ptr<apache::thrift::transport::TSocket> sock =
      std::dynamic_pointer_cast<apache::thrift::transport::TSocket>(
          connInfo.transport);
  spdlog::info("ThriftServer Incoming: {} {}", sock->getSocketInfo(),
               sock->getPeerHost());
  return new ThriftServer(college, broadcast, sock->getPeerAddress());
}

void ThriftServer::GetPreviewImageInfo(std::string &_return,
                                       const std::string &uuid) {
  _return = college->GetPreviewImgInfo(uuid);
}
